function f_opt_error_log()
{   #执行sql报错后打印执行错误的日志
    declare _sql_exe_error
    _sql_exe_error="${2}"
    if [ "${1}x" == "showx" ]
    then
        { [ -f "${_sql_exe_error}" ] && sed '/Using a password on the command /d' ${_sql_exe_error}|tee -a ${log_file};} || true
    fi
    [ -f "${_sql_exe_error}" ] &&  rm -f ${_sql_exe_error}
    return 0
}

function f_exe_sql_in_mysql()
{   #在线上环境执行sql的函数
    declare exe_sql exe_host exe_port exe_role _exe_state mysql_slave_comm _sql_exe_error pfunc_name
    exe_sql="${1}"
    exe_host="${2}"
    exe_port="${3}"
    exe_role="${4}"
    pfunc_name="${5}"
    _sql_exe_error="${osc_log_dir}/${user_order_id}.$(date +%N%s)"
    if [ "${exe_role}x" == "slavex" ]
    then
        #在master/或者slave上执行使用-NB
        mysql_slave_comm="mysql --default-character-set=utf8 -u${mysql_user} -p${mysql_pass} -h${exe_host} -P${exe_port}"
        timeout ${exe_time} ${mysql_slave_comm} ${mysql_db} -NBe "${exe_sql}" 2> ${_sql_exe_error} && _exe_state="${?}" || _exe_state="${?}"
    elif [ "${exe_role}x" == "slaveNx" ]
    then
        #在master/或者slave上执行没有使用-NB
        mysql_slave_comm="mysql --default-character-set=utf8 -u${mysql_user} -p${mysql_pass} -h${exe_host} -P${exe_port}"
        timeout ${exe_time} ${mysql_slave_comm} ${mysql_db} -e "${exe_sql}" 2> ${_sql_exe_error} && _exe_state="${?}" || _exe_state="${?}"
    elif [ "${exe_role}x" == "nodbx" ]
    then
        #在master/或者slave上执行没有使用指定db
        timeout ${exe_time} ${mysql_comm} ${mysql_db} -NBe "${exe_sql}" 2> ${_sql_exe_error} && _exe_state="${?}" || _exe_state="${?}"
    else
        #在master上执行
        timeout ${exe_time} ${mysql_comm} ${mysql_db} -NBe "${exe_sql}" 2> ${_sql_exe_error} && _exe_state="${?}" || _exe_state="${?}"
    fi

    if [ ${_exe_state} -eq 124 ]
    then #124表示执行超时
        [ -z "${exe_host}" ] && exe_host="${mysql_host}"
        __="select * from ${isdbp} where (time >= (select max(time) from ${isdbp} where state = 'Waiting for table metadata lock')"
        __="${__} or state = 'Waiting for table metadata lock') and command != 'Sleep' and info is not NULL;"
        f_logging "$(eval echo ${log_addr}):REMAIND" "'${mysql_host}:${mysql_port}' 当前'${FUNCNAME[1]}'任务信息如下" "1" "0" "0"
        timeout ${exe_time} ${mysql_comm} ${mysql_db} -NBe "${__}" 2>/dev/null|awk -F'\t' '{
                    print "[ ID : "$1" ] [ USER : "$2" ] [ IP : "$3" ] [ DB : "$4" ] [ COMMAND : "$5" ] [ TIME : "$6" ] [ STATE : "$7" ] [ SQL : "$8" ]"}'
        __="select concat('kill ',id,';') from ${isdbp} where state = 'Waiting for table metadata lock' and db = '${mysql_db}' and user = '${mysql_user}'"
        __="${__} and host like '${localhost_ip}:%' and (info like 'alter%${_t_}%' or info like 'ALTER%${_t_}%' or info like 'Alter%${_t_}%')"
        timeout ${exe_time} ${mysql_comm} ${mysql_db} -NBe "${__}" 2>/dev/null || true
    elif [ ${_exe_state} -ne 0 ]
    then
        [ -z "${exe_host}" ] && exe_host="${mysql_host}"
        __="$(f_opt_error_log "show" "${_sql_exe_error}")"
        f_logging "$(eval echo ${log_addr}):ERROR" "'${exe_host}:${mysql_port} ${mysql_db}' 执行遇到错误, ] [ ${__} ] [ ${FUNCNAME[1]}:${exe_sql}" "2"
    else
        f_opt_error_log "del" "${_sql_exe_error}"
    fi

    [ -f "${_sql_exe_error}" ] &&  rm -f ${_sql_exe_error}
    return "${_exe_state}"
}

function f_exe_sql_in_test_mysql()
{   #在测试环境执行mysql操作
    declare test_exe_sql _exe_state _test_db_ _sql_exe_error pfun_name
    _sql_exe_error="${osc_log_dir}/${user_order_id}.$(date +%N%s)"
    test_exe_sql="${get_global_sql_mode};${1}"
    _test_db_="${2}"
    pfun_name="${3}"
    [ -z "${_test_db_}" ] && _test_db_="${test_db}"
    timeout ${exe_time} ${test_mysql_comm} ${_test_db_} -NBe "${test_exe_sql}" 2> ${_sql_exe_error} && exe_state="${?}" || exe_state="${?}"
    _exe_state="${exe_state}"
    if [ ${exe_state} -eq 124 ]
    then
        timeout ${exe_time} ${test_mysql_comm} ${_test_db_} -NBe "show processlist" >> ${log_file} 2>/dev/null || true
        __="${fun_name}:在 ${test_host}:${test_port}, 执行${exe_time}超时, 超时原因可能是受阻塞, 可以查阅 '${log_file}' 进行分析"
        f_logging "${pfun_name}:WARN" "${__} ] [ ${FUNCNAME[1]}:${test_exe_sql}" "2"
    elif [ ${exe_state} -ne 0 ]
    then
        __="$(f_opt_error_log "show" "${_sql_exe_error}")"
        f_logging "${pfun_name}:ERROR" "'${test_host}:${test_port} ${_test_db_}' 执行遇到错误 ] [ ${__} ] [ ${test_exe_sql}" "2" >&2
    else
        f_opt_error_log "del" "${_sql_exe_error}"
    fi
    [ -f "${_sql_exe_error}" ] &&  rm -f ${_sql_exe_error}
    return "${_exe_state}"
}

function f_exe_sql_to_dbadb()
{
    declare sql_dbadb error_info _tmp_ fun_name
    sql_dbadb="$(tr "\n" " "<<< "${1}"|awk '{for(i=1;i<=NF;i++){if(i==NF){printf $i}else{printf $i" "}}printf"\n"}')"
    fun_name="${2}"
    exe_state="0"
    error_info="$({ timeout ${exe_time} ${mysql_comm_dbadb} -NBe "${sql_dbadb}" >/dev/null;} 2>&1|sed '/Using a password on/d')" || exe_state="${?}"
    if [ "${exe_state}x" != "0x" ]
    then
        _tmp_="访问${dbadb_db} 执行sql遇到错误 ] [ ${sql_dbadb} ] [ ${error_info}"
        f_logging "$(eval echo ${log_addr}):ERROR" "${fun_name}:${_tmp_}"
        return 1
    else
        _tmp_="${sql_dbadb}"
        f_logging "$(eval echo ${log_addr}):INFO" "${fun_name}:访问${dbadb_db} ] [ ${fun_name}:${_tmp_}"
    fi
    return 0
}


